//管理用户数据相关
import {defineStore} from "pinia";
import {ref} from "vue";
import {deductMoneyAPI, loginAPI, registerAPI} from "@/api/user";
export const useUserStore=defineStore('user',()=>{

    //1。定义管理用户数据的state
    let userInfo=ref({})
    let addressbyId=ref({})
    let addressListbyUserId=ref({})

    // const cartStore=useCartStore()
    //2. 定义获取接口数据的action函数
    const getUserInfo = async (loginFormDTO) => {
        const res = await loginAPI(loginFormDTO)
        userInfo.value=res
        return res
    }

    const userRegister = async (loginFormDTO) => {
        const res = await registerAPI(loginFormDTO)
        return res
    }

    const deuctMoney=async (pw,amount) =>{
        const res=await deductMoneyAPI(pw,amount)
        console.log("deuctMoneyAPIres=",res)
    }


    const clearUserInfo=()=>{
        userInfo.value={}
        sessionStorage.clear()
        //执行清除购物车action
        // cartStore.clearCart()
    }


    //3. 以对象的格式把state和action return
    return{
        userInfo,
        getUserInfo,
        deuctMoney,
        clearUserInfo,
        userRegister
    }
},{
    persist:true
})
